package operation;

import book.Book;
import book.Booklist;

import java.util.Scanner;

public class Delete implements IOperation{
    @Override
    public void work(Booklist booklist) {
        System.out.print("请输入要删除的图书名：");
        Scanner c = new Scanner(System.in);
        String name = c.nextLine();
        Book book = booklist.getBook(0);
        for (int i = 0; i < booklist.getNum(); i++) {
            book = booklist.getBook(i);
            if (book.getName().equals(name)) {
                System.out.println("删除成功！");
                booklist.setNum(booklist.getNum()-1);
                Book tmp = null;
                for (int j = i; j < booklist.getNum(); j++) {
                    tmp = booklist.getBook(j+1);
                    booklist.setBook(i,tmp);
                }
                booklist.setBook(booklist.getNum(), null);
            } else if (i == booklist.getNum()-1) {
                System.out.println("没有找到相关图书，请重新尝试。");
            }
        }
    }
}
